﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography.X509Certificates;
namespace Codinfinity.BeIDApplet
{
    public class Identity
    {
        public static IdentityObject GetIdentity()
        {
            return GetIdentity(SessionManager.SessionID);
        }

        public static IdentityObject GetIdentity(Guid aSessionID)
        {
            IdentityObject Object = new IdentityObject();
            foreach (var p in typeof(IdentityObject).GetProperties())
            {
                if (p.PropertyType.Equals(typeof(string)))
                    p.SetValue(Object, Codinfinity.BeIDApplet.Identity.GetVariable(aSessionID, "Beid." + p.Name), null);
                if (p.PropertyType.Equals(typeof(bool)))
                    p.SetValue(Object, Codinfinity.BeIDApplet.Identity.GetVariable(aSessionID, "Beid." + p.Name)=="True", null);

                    
            }
            return Object;
        }
        
        public static string GetVariable(Guid aSessionID, string variableName)
        {
            if (!SessionManager.GetClientSession(aSessionID).KeyExist(variableName))
                return "";
            else
                return (string)SessionManager.GetClientSession(aSessionID)[variableName];
        }
        public static object GetObject(Guid aSessionID, string variableName)
        {
            if (!SessionManager.GetClientSession(aSessionID).KeyExist(variableName))
                return "";
            else
                return SessionManager.GetClientSession(aSessionID)[variableName];
        }
        public static void SetVariable(Guid aSessionID, string variableName, string value)
        {
            if (string.IsNullOrEmpty(value))
            {
                SessionManager.GetClientSession(aSessionID)[variableName] = null;
                if (SessionManager.GetClientSession(aSessionID).KeyExist(variableName))
                    SessionManager.GetClientSession(aSessionID).Remove(variableName);
            }
            else
            {
                SessionManager.GetClientSession(aSessionID)[variableName] = value;
            }
        }
        public static void SetObject(Guid aSessionID, string variableName, object value)
        {
            if (value==null)
            {
                SessionManager.GetClientSession(aSessionID)[variableName] = null;
                if (SessionManager.GetClientSession(aSessionID).KeyExist(variableName))
                    SessionManager.GetClientSession(aSessionID).Remove(variableName);
            }
            else
            {
                SessionManager.GetClientSession(aSessionID)[variableName] = value;
            }
        }

        private static string GetVariable(string variableName)
        {
            return GetVariable(SessionManager.SessionID, variableName);
        }
        public static object GetObject(string variableName)
        {
            return GetObject(SessionManager.SessionID, variableName);
        }
        private static void SetVariable(string variableName, string value)
        {
            SetVariable(SessionManager.SessionID, variableName, value);
        }
        private static void SetObject(string variableName, object value)
        {
            SetObject(SessionManager.SessionID, variableName, value);
        }

        public static string NationalNumber{
            get { return GetVariable("Beid.NationalNumber");}
            set { SetVariable("Beid.NationalNumber",value); ; }
        }

        public static byte[]Photo{
            get { return Convert.FromBase64String("Beid.Photo");}
            set { SetVariable("Beid.Photo", Convert.ToBase64String(value)); }
        }

        public static string Gender
        {
            get { return GetVariable("Beid.Gender"); }
            set { SetVariable("Beid.Gender", value); }
        }
        public static string Nationality{
            get { return GetVariable("Beid.Nationality"); }
            set { SetVariable("Beid.Nationality", value); ; }
        }
        public static string PlaceOfBirth{
            get { return GetVariable("Beid.PlaceOfBirth");}
            set { SetVariable("Beid.PlaceOfBirth",value);}
        }
        public static string DateOfBirth{
            get { return GetVariable("Beid.DateOfBirth");}
            set { SetVariable("Beid.DateOfBirth",value);}
        }
        public static string Address{
            get { return GetVariable("Beid.Address");}
            set { SetVariable("Beid.Address",value);}
        }
        public static string Locality{
            get { return GetVariable("Beid.Locality");}
            set { SetVariable("Beid.Locality",value);}
        }
        public static string PostalCode{
            get { return GetVariable("Beid.PostalCode");}
            set { SetVariable("Beid.PostalCode",value);}
        }
        public static string FirstName{
            get { return GetVariable("Beid.FirstName");}
            set { SetVariable("Beid.FirstName",value);}
        }
        public static string LastName{
            get { return GetVariable("Beid.LastName");}
            set { SetVariable("Beid.LastName",value);}
        }
        public static string Age{
            get { return GetVariable("Beid.Age");}
            set { SetVariable("Beid.Age",value);}
        }
        public static bool Identified{
            get { return GetVariable("Beid.Identified")=="True";}
            set { SetVariable("Beid.Identified",value.ToString());}
        }
        public static string Hash{
            get { return GetVariable("Beid.Hash");}
            set { SetVariable("Beid.Hash",value);}
        }

        public static X509Certificate2 AuthnCertificate {
            get { return (X509Certificate2)GetObject("Beid.AuthnCertificate"); }
            set { SetObject("Beid.AuthnCertificate",value);}
        }

        public static X509Certificate2 SignCertificate
        {
            get { return (X509Certificate2)GetObject("Beid.SignCertificate"); }
            set { SetObject("Beid.SignCertificate", value); }
        }
    }
}
